package com.itheima.java8Time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: Java8TimeClassMethodDemo1
 * @Description:Date-Time API中的所有类均生成不可变实例，它们是线程安全的，并且这些类不提供公共构造函数，
 * 也就是说没办法通过new的方式直接创建，需要采用工厂方法加以实例化
 *
 * now方法可用根据当前日期或时间创建实例对象
 * @author: Yrd
 * @date: 2021-10-07 19:22
 */
public class Java8TimeClassMethodDemo1 {
    
    public static void main(String[] args){
        ////使用now方法创建Instant的实例对象
        //Instant instantNow = Instant.now();
        ////使用now方法创建LocalDate的实例对象
        //LocalDate localDate = LocalDate.now();
        ////使用now方法创建LocalTime的实例对象
        //LocalTime localTime = LocalTime.now();
        ////使用now方法创建LocalDateTime的实例对象
        //LocalDateTime localDateTime = LocalDateTime.now();
        ////使用now方法创建zonedDateTime的实例对象
        //ZonedDateTime zonedDateTime = ZonedDateTime.now();
        //
        ////将实例对象打印到控制台查看内容
        //System.out.println("Instant:"+instantNow);
        //System.out.println("localDate:"+localDate);
        //System.out.println("localTime:"+localTime);
        //System.out.println("localDateTime:"+localDateTime);
        //System.out.println("zonedDateTime:"+zonedDateTime);
        //
        //System.out.println("===================================");
        //Year year=Year.now();
        //YearMonth yearMonth = YearMonth.now();
        //MonthDay monthDay = MonthDay.now();
        //System.out.println("year:"+year);
        //System.out.println("yearMonth:"+yearMonth);
        //System.out.println("monthDay:"+monthDay);
        //
        //System.out.println("==================================");
        //System.out.println("如果秒和纳秒是0的话，那么默认不会封装这些数据，只显示时分");
        //LocalDate localDate1 = LocalDate.of(2019,11,20);
        //System.out.println("localdate1:"+localDate1);
        //
        ////如果秒和纳秒是0的话，那么默认不会封装这些数据，只显示时分
        //LocalTime localTime1 = LocalTime.of(20,30,10);
        //System.out.println("localtime1:"+localTime1);
        //
        //LocalDateTime localDateTime1 = LocalDateTime.of(2019,9,29,13,20,0);
        //System.out.println("localDateTime1:"+localDateTime1);

        //System.out.println("================================");
        //
        ////获取所有的时区信息
        //Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        //for (String zoneId : zoneIds) {
        //    System.out.println(zoneId);
        //}

        ////查看当前系统的默认时区信息
        //ZoneId zoneId = ZoneId.systemDefault();
        //System.out.println(zoneId);

        //System.out.println("===================================");
        //
        //System.out.println("为LocalDateTime添加时区信息");
        //LocalDateTime localDateTime = LocalDateTime.of(2021,10,10,10,20,30);
        //ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        //System.out.println(zonedDateTime);
        //
        ////更改时区查看其他时区的当前时间，通过withZoneSameInstant（）方法即可更改.
        //ZonedDateTime tokyoZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
        //System.out.println("在同一时刻，Asia/Tokyo的时间是："+tokyoZonedDateTime);

        //System.out.println("===========================================");
        //
        //LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JANUARY, 11, 11, 11, 11);
        //
        //System.out.println(localDateTime);

        //Month month = Month.of(12);
        //System.out.println(month);
        //
        //LocalDateTime dateTime = LocalDateTime.of(2021, month, 11, 11, 11, 11);
        //System.out.println(dateTime);
        //
        //System.out.println(Month.JANUARY.ordinal());

        //System.out.println("============================");
        //LocalDateTime now = LocalDateTime.now();
        //System.out.println(now);

        //LocalDateTime plusNanos1 = now.plusNanos(200);
        //System.out.println(plusNanos1);

        //LocalDateTime localDateTime = LocalDateTime.of(2021, Month.APRIL, 11, 11, 11, 11,200);
        //System.out.println("当前时间："+localDateTime);

        //LocalDateTime date1 = localDateTime.plusDays(4);
        //System.out.println(localDateTime==date1);

        //System.out.println("当前时间的4天后时间："+date1);
        //
        //LocalDateTime date2 = localDateTime.minusMonths(2);
        //System.out.println("当前时间减两个月 的时间是："+date2);
        //
        //LocalDateTime plusNanos = localDateTime.plusNanos(500);
        //System.out.println("当前时间增加500纳秒后时间："+plusNanos);

        //System.out.println("==========================");
        //
        ////Period 表示一段时间
        //LocalDateTime localDateTime = LocalDateTime.now();
        //Period period = Period.of(2, 3, 10);
        //
        //LocalDateTime endDateTime = localDateTime.plus(period);
        //System.out.println(endDateTime);

        //System.out.println("==================");
        //
        //LocalDateTime localDateTime = getTime();
        //System.out.println(localDateTime);//2021-10-10T10:10:10
        //
        ////调整日期，日的日期2021-10-02T10:10:10
        //LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(2);
        //System.out.println(localDateTime1);
        //
        //LocalDateTime localDateTime2 = localDateTime.with(ChronoField.DAY_OF_MONTH, 2);
        //System.out.println(localDateTime2);//2021-10-02T10:10:10

        //System.out.println("====================");
        //LocalDateTime localDateTime = LocalDateTime.now();
        //System.out.println(localDateTime);//2021-10-10T23:16:20.911(周日)
        //
        ////将时间调整为下个月的第一天
        //LocalDateTime with = localDateTime.with(TemporalAdjusters.firstDayOfNextMonth());
        //System.out.println(with);//2021-11-01T23:16:20.911
        //
        ////将时间调整为,下一个,周五
        //LocalDateTime with1 = localDateTime.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        //System.out.println(with1);//2021-10-15T23:16:20.911
        //
        ////将时间调整为,上一个,周三
        //LocalDateTime with2 = localDateTime.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
        //System.out.println(with2);//2021-10-06T23:16:20.911

        //System.out.println("==============================");
        //
        //LocalDate payDay = LocalDate.of(2021,11,10);
        ////计算payDay的真实发薪日是什么时候
        //LocalDate realPayDay = LocalDate.from(new PayDayAdjuster().adjustInto(payDay));
        //
        //System.out.println("预计的发薪日是："+payDay+",真实的发薪日是："+realPayDay);
        //
        //LocalDate localDate = LocalDate.from(LocalDate.of(2021,11,10));
        //System.out.println("localDate:"+localDate);
        //LocalDate with = localDate.with(new PayDayAdjuster());
        //System.out.println(with);

        //System.out.println("==============================");

        //
        //Date date = new Date();
        //
        //System.out.println(date);
        //System.out.println(date.getTime());
        ////System.out.println(System.currentTimeMillis());
        //date.setTime((date.getTime()/1000)*1000);
        //System.out.println(date);
        //System.out.println(date.getTime());
        //;
        ////1634182264 1634182285 1634182295
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        //String formatDate = simpleDateFormat.format(date.getTime()/1000);
        //
        //System.out.println(formatDate);
        //try {
        //    System.out.println((simpleDateFormat.parse(formatDate)).getTime());
        //} catch (ParseException e) {
        //    e.printStackTrace();
        //}

        ////System.out.println("==============================");
        ////1.获取某一天距离下一个劳动节相隔天数
        //LocalDate now = LocalDate.now();
        ////2.调用now的query方法，然后将我们自己实现类utilDayQueryImpl作为参数传入即可。
        //Long day = now.query(new UtilDayQueryImpl());
        //System.out.println("当前的时间是："+now+"，距离下一个劳动节还有"+day+"天。");

        ////System.out.println("==============================");
        ////一、使用Instant类将java.util.Date转换为java.time.LocalDate
        ////初始化Date对象
        //Date d = new Date();
        ////1.将Date对象转换为Instant对象
        //Instant i = d.toInstant();
        ////2.Date、类包含日期和时间信息，但是并不提供时区信息，和Instant类一样，
        ////通过Instant类的atZone方法添加时区信息进行转换.
        //ZonedDateTime zonedDateTime = i.atZone(ZoneId.systemDefault());
        ////3.将ZonedDateTime通过toLocalDate方法转换为LocalDate对象.
        //LocalDateTime localDate = zonedDateTime.toLocalDateTime();
        //System.out.println("转换之前Date对象："+d);
        //System.out.println("ZonedDateTime对象："+zonedDateTime);
        //System.out.println("转换之后LocalDate对象："+localDate);
        //LocalDateTime now = LocalDateTime.now();
        //System.out.println("now:"+now);

        ////二、将java.util.Date转换为java.time.LocalDate
        ////java.sql.Date类提供了转换为LocalDate的方法，那么可以将java.util.Date先
        ////      转换为java.sql.Date,通过java.sql.Date提供的方式转换为LocalDate.
        ////java.sql.Date类构造的时候需要毫秒值->java.util.Date类中提供了一个提供毫秒值
        ////      的方法，getTime.
        //Date d1 = new Date();
        //java.sql.Date date = new java.sql.Date(d1.getTime());
        //LocalDate localDate1 = date.toLocalDate();

        System.out.println("==============================");

        //对LocalDate进行格式化和解析，初始化LocalDateTime对象.
        LocalDateTime now = LocalDateTime.now();
        //now对象可以直接调用format方法进行格式化。
        String s1 = now.format(DateTimeFormatter.ISO_DATE_TIME);
        String format = now.format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("localDate:"+now);
        System.out.println("s1:"+s1);
        System.out.println("format:"+format);
        //解析字符串日期
        LocalDateTime parse = LocalDateTime.parse(s1);
        System.out.println("parse:"+parse);
        LocalDate parse1 = LocalDate.parse(format,DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("parse1:"+parse1);

    }

    private static LocalDateTime getTime() {
        return LocalDateTime.of(2021,10,10,10,10,10);
    }
}

































